#include "Cats/CatStates/CatState.h"
#include "Cats/CatStates/CatState_Witch_Skill.h"
#include "Cats/CatSpriteLoader.h"
#include "Cats/BaseCat.h"
#include "Cats/PunkCat.h"

#include "Supporting/SoundLoader.h"
#include "Supporting/Enums/EnumSoundType.h"

CatState_Witch_Skill::CatState_Witch_Skill(BaseCat* pCatRef):CatState(pCatRef)
{
	_exitState = false;
}

CatState_Witch_Skill::~CatState_Witch_Skill()
{

}

void CatState_Witch_Skill::runOnEnter()
{
	//do not exit state yet
	_exitState = false;
	// Sound
	SoundLoader::playSound(WITCH_SKILL_SOUND);

	_summonAnimationDuration = CatSpriteLoader::getAnimationDuration(_pCatRef->getCatType(), CAT_ANIM_SKILL);

	_pCatRef->stopAllActions();
	_pCatRef->runAction(CatSpriteLoader::getCatAnimation(_pCatRef->getCatType(), CAT_ANIM_SKILL));
}

void CatState_Witch_Skill::update(float timeDiff)
{
	_summonAnimationDuration -= timeDiff;

	if (!_exitState && _summonAnimationDuration <= 0) //guard against resume re-run
	{
		_exitState = true;
		summonCats();
	}

	//animation complete, summon cats
	if (_exitState)
	{
		_pCatRef->setNextCatState(CAT_CHASE);
	}
}

void CatState_Witch_Skill::onExit()
{
	CatState::onExit();
	// Sound
	SoundLoader::endSound(WITCH_SKILL_SOUND);

	_pCatRef->startCoolDownTimer();
}

void CatState_Witch_Skill::onPause()
{
	CatState::onPause();
	_exitState = true; //force cancellation of skill casting
}

void CatState_Witch_Skill::summonCats()
{
	PunkCat* summonedCat;
	int summonPos;

	//above this cat
	summonPos = _pCatRef->getPositionY() + _pGameMapRef->getPixelsPerGrid();
	if (summonPos < _pGameMapRef->getScreenTopBound()) //check top bound
	{
		summonedCat = PunkCat::createCat();
		summonedCat->spawnBySummon(CCPoint(_pCatRef->getPositionX(), summonPos));
		_pCatsManagerRef->addCat(summonedCat);
	}

	//below this cat
	summonPos = _pCatRef->getPositionY() - _pGameMapRef->getPixelsPerGrid();
	if (summonPos > _pGameMapRef->getScreenBottomBound())//check bottom bound
	{
		summonedCat = PunkCat::createCat();
		summonedCat->spawnBySummon(CCPoint(_pCatRef->getPositionX(), _pCatRef->getPositionY() - _pGameMapRef->getPixelsPerGrid()));
		_pCatsManagerRef->addCat(summonedCat);
	}

	//left of this cat
	summonedCat = PunkCat::createCat();
	summonedCat->spawnBySummon(CCPoint(_pCatRef->getPositionX() - _pCatRef->boundingBox().size.width/2, _pCatRef->getPositionY()));
	_pCatsManagerRef->addCat(summonedCat);

	//right of this cat
	summonedCat = PunkCat::createCat();
	summonedCat->spawnBySummon(CCPoint(_pCatRef->getPositionX() + _pCatRef->boundingBox().size.width/2, _pCatRef->getPositionY()));
	_pCatsManagerRef->addCat(summonedCat);
}
